﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.model;
using l1cs.server.datatables;
using l1cs.server.templates;
using l1cs.server.serverpackets;

namespace l1cs.server.clientpackets
{
    public class C_Drawal : ClientBasePacket
    {
        private const String _C_DRAWAL = "[C] C_Drawal";
        private static Logger _log = Logger.getLogger( typeof( C_Drawal ).FullName );

        public C_Drawal( byte[] abyte0, ClientThread clientthread )
            : base( abyte0 )
        {
            int i = readD();
            int j = readD();

            L1PcInstance pc = clientthread.getActiveChar();
            L1Clan clan = L1World.getInstance().getClan( pc.getClanname() );
            if ( clan != null )
            {
                int castle_id = clan.getCastleId();
                if ( castle_id != 0 )
                {
                    L1Castle l1castle = CastleTable.getInstance().getCastleTable(
                            castle_id );
                    int money = l1castle.getPublicMoney();
                    money -= j;
                    L1ItemInstance item = ItemTable.getInstance().createItem(
                            L1ItemId.ADENA );
                    if ( item != null )
                    {
                        l1castle.setPublicMoney( money );
                        CastleTable.getInstance().updateCastle( l1castle );
                        if ( pc.getInventory().checkAddItem( item, j ) == L1Inventory.OK )
                        {
                            pc.getInventory().storeItem( L1ItemId.ADENA, j );
                        }
                        else
                        {
                            L1World.getInstance().getInventory( pc.getX(),
                                    pc.getY(), pc.getMapId() ).storeItem(
                                    L1ItemId.ADENA, j );
                        }
                        pc.sendPackets( new S_ServerMessage( 143, "$457", "$4" + " ("
                                + j + ")" ) );
                    }
                }
            }
        }

        override
        public String getType()
        {
            return _C_DRAWAL;
        }
    }
}
